package com.yxy.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

public class getNetSource {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 String urlString = "http://www.163.com";
		 // open reader for URL
        try {
        	URL url = new URL(urlString); 
        	InputStream in = url.openStream();
			File file = new File("index.html");
			
			 // save all employee records to the file employee.dat
	         FileOutputStream fos = new FileOutputStream(file);
	         BufferedOutputStream bos = new BufferedOutputStream(fos);
	         BufferedInputStream bis = new BufferedInputStream(in);
	         InputStreamReader inStreamReader = new InputStreamReader(in);
	         
	         FileWriter fw = new FileWriter(file); 
	         int tempchar = 0;
	         byte[] b = new byte[1024];
//	        while((tempchar=in.read(b))!= -1){
//	        		bos.write(b, 0, tempchar);
//	        		// bos.write((char) tempchar);
//	         }
	        
//	        	 fw.write(b , 0, tempchar);
	          
	        	 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
	        	 StringBuffer sb = new StringBuffer();
	        	 String line = null;
	        	 while ((line = reader.readLine()) != null){
	        		 
		        	 fw.write(line, 0, line.length()) ;
		        	 fw.write(System.getProperty("line.separator"));
	        	 }
	         
	        bos.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static String inputStream2String(InputStream is)
			throws UnsupportedEncodingException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,
				"utf-8"));
		StringBuffer sb = new StringBuffer();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
